﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.Utils;
using Telerik.Windows.Controls.GridView;


namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    /// <summary>
    /// 初始数据主表  
    /// </summary>
    /// Title:ZMCM82
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张坤
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张坤   [创建]
    ///
    public partial class ApplicationDetailInsert : EFViewCmes
    {
        private MonthBlock monthblock = new MonthBlock();//构造前台下拉列表
        public ApplicationDetailInsert(String mode)
        {
            InitializeComponent();
            base.Mode = OperationMode.Insert;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 执行保存动作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Confirmed_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            String msg = "1";
            IList<EIRow> _list = this.sub_grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.sub_grid.GetCheckedEIRows();
            if (this.LessMoney.Value.ToString() == "0")
            {
                msg = "用户已经无材料费用额度，请通知相关部门调整费用额度！";
            }
            else if (_list.Count <= 0)
            {
                msg = "请选择需要申请上报的数据明细！";
            }
            //else
            //{
            //    for (int i = 0; i < _list.Count; i++)
            //    {
            //        if (this.sub_grid.GetCheckedEIRows()[i]["fdApplyProject"] != null)
            //        {
            //            if (this.sub_grid.GetCheckedEIRows()[i]["fdApplyProject"].ToString() == "")
            //            {
            //                msg = "请选择适用工程！";
            //                break;
            //            }
            //        }
            //        else
            //        {
            //            msg = "请选择适用工程！";
            //            break;
            //        }
            //    }
            //}

            if (msg == "1")
            {
                base.Submit(OperationMode.Insert, "ZMCM81", "insert", DataRange.CheckedItems, DoCommitCompleted, null);
            }
            else
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }

        }

        /// <summary>
        /// 执行完新增操作的回调函数
        /// </summary>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {

            if (e.EIInfo.Status == -1)
            {
                String msg = "插入失败！";
                if (e.EIInfo.GetMsg() != null || e.EIInfo.GetMsg().Equals(""))
                {
                    msg = e.EIInfo.Msg;
                }
                base.OpenEFWindowAlert(msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = true;
                _window.Close();
            }
        }

        /// <summary>
        /// 列表数据编辑结束后执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sub_grid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
        {
            EFGridTextColumn column = e.Cell.Column as EFGridTextColumn;
            if (column != null)
            {
                if (column.eColumnName.Equals("fdApplicationNumber"))
                {
                    UIBaseEntity obj = e.Cell.ParentRow.DataContext as UIBaseEntity;
                    EIRow row = obj.GetEIRow();
                    var price = row["fdPrice"];
                    row["fdApplicationSum"] = Convert.ToDouble(price) * Convert.ToDouble(row["fdApplicationNumber"]);
                }
            }
        }

        private void QueryClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCM82");
            eiInfo.Set("method", "query");
            EIBlock block = eiInfo.AddBlock(EIConstants.QueryStatusBlock);

            EIRow row = new EIRow();
            EIColumn column = new EIColumn();
            column.SetName("fdYear");
            block.AddColumn(column);
            row.Add("fdYear", this.fdYear.Value);

            column = new EIColumn();
            column.SetName("fdMonth");
            block.AddColumn(column);
            row.Add("fdMonth", this.fdMonth.Value);

            column = new EIColumn();
            column.SetName("fdApplicationDepartment");
            block.AddColumn(column);
            row.Add("fdApplicationDepartment", this.fdApplicationDepartment.Value);

            if (this.inqufdMaterialName.Value != null && !this.inqufdMaterialName.Value.Equals(""))
            {
                column = new EIColumn();
                column.SetName("fdMaterialName");
                block.AddColumn(column);

                row.Add("fdMaterialName", this.inqufdMaterialName.Value);
            }

            if (this.inqufdMaterialCode.Value != null && !this.inqufdMaterialCode.Value.Equals(""))
            {
                column = new EIColumn();
                column.SetName("fdMaterialCode");
                block.AddColumn(column);

                row.Add("fdMaterialCode", this.inqufdMaterialCode.Value);
            }

            if (this.inqufdSpecModel.Value != null && !this.inqufdSpecModel.Value.Equals(""))
            {
                column = new EIColumn();
                column.SetName("fdSpecModel");
                block.AddColumn(column);

                row.Add("fdSpecModel", this.inqufdSpecModel.Value);

            }

            block.AddRow(row);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoQueryCompleted);
        }

        /// <summary>
        /// 查询后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoQueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                this.sub_grid.UpdateData(e.EIInfo);
            }
        }
    }
}
